package com.sinothk.special.buyer.userblog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.base.model.BizUserInfo;
import com.sinothk.special.buyer.userblog.entity.UserBlog;
import com.sinothk.special.buyer.userblog.entity.UserBlogAddVO;
import com.sinothk.special.buyer.userblog.entity.UserBlogDTO;
import com.sinothk.special.buyer.userblog.entity.UserBlogSearchVO;
import com.sinothk.special.buyer.userblog.mapper.UserBlogMapper;
import com.sinothk.special.buyer.userblog.service.IUserBlogService;
import com.sinothk.special.buyer.userblog.service.IUserBlogTipService;
import com.sinothk.special.user.service.IBizUserInfoService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 个人博客
 * @Author: Liangyt
 * @Date: 2022-03-30
 * @Version: V1.0
 */
@Service
public class UserBlogServiceImpl extends ServiceImpl<UserBlogMapper, UserBlog> implements IUserBlogService {

    @Resource
    private UserBlogMapper userBlogMapper;

    @Autowired
    private IBizUserInfoService bizUserInfoService;

    @Autowired
    private IUserBlogTipService userBlogTipService;

    @Override
    public void saveVo(UserBlogAddVO userBlogVo) {
        UserBlog userBlog = new UserBlog();
        BeanUtils.copyProperties(userBlogVo, userBlog);
        userBlog.setId(UUIDGenerator.generate());

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        userBlog.setUid(loginUser.getId());
        userBlog.setBlogMediaType(0);
        if (userBlog.getBlogMediaUrl() != null && userBlog.getBlogMediaUrl().length() > 0) {
            String[] mediaArr = userBlog.getBlogMediaUrl().split(",");
            userBlog.setBlogMediaCover(mediaArr[0]);
        }
        this.save(userBlog);

        // 更新用户信息
        BizUserInfo bizUserInfo = new BizUserInfo();
        bizUserInfo.setId(loginUser.getId());

        // 博客信息
        UserBlog userBlogTemp = new UserBlog();
        userBlogTemp.setBlogMediaType(userBlog.getBlogMediaType());
        userBlogTemp.setBlogTxt(userBlog.getBlogTxt());
        userBlogTemp.setBlogMediaUrl(userBlog.getBlogMediaUrl());
        userBlogTemp.setBlogMediaCover(userBlog.getBlogMediaCover());
        bizUserInfo.setBlogLatestJson(JSON.toJSONString(userBlogTemp));
        bizUserInfoService.updateBizUser(bizUserInfo);

        // 新增发布提醒
        userBlogTipService.addTip(userBlog.getId(), userBlog.getUid());
    }

    /**
     * * 查询-附近热门博客
     *
     * @param page
     * @param searchVO
     * @return
     */
    @Override
    public IPage<UserBlogDTO> listForNearbyAndHotBlogs(Page<UserBlogDTO> page, UserBlogSearchVO searchVO) {
        return userBlogMapper.listForNearbyAndHotBlogs(page, searchVO);
    }

    /**
     * 查询-个人博客
     *
     * @param page
     * @param searchVO
     * @return
     */
    @Override
    public IPage<UserBlogDTO> listBlogsForUser(Page<UserBlogDTO> page, UserBlogSearchVO searchVO) throws Exception {
        if (StringUtils.isBlank(searchVO.getUid())) throw new Exception("uid不能为空");
        return userBlogMapper.listBlogsForUser(page, searchVO);
    }

    /**
     * @param page
     * @param searchVO
     * @return
     */
    @Override
    public IPage<UserBlogDTO> listBlogsForILike(Page<UserBlogDTO> page, UserBlogSearchVO searchVO) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        searchVO.setUid(loginUser.getId());
        return userBlogMapper.listBlogsForILike(page, searchVO);
    }
}
